package com.witmob.drivers;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

	private EditText carLicense;
	
	private EditText engineModel;

	private TextView resultView;

	private ProgressDialog dialogs;

	private String resultMessage;

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			dialogs.dismiss();
			if (resultMessage == null || resultMessage.equals("")) {

				resultMessage = "没有违章";
			}
			resultView.setText(resultMessage);
		}

	};

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		carLicense = (EditText) findViewById(R.id.car_license);
		engineModel = (EditText) findViewById(R.id.engine_model);

		resultView = (TextView) findViewById(R.id.theresult);

	}

	public void getResult(View view) {
		dialogs = ProgressDialog
				.show(MainActivity.this, "", "Loading...", true);
		new Thread(new Runnable() {

			@Override
			public void run() {
				VehiclesInfo vehiclesInfo = new VehiclesInfo();
				vehiclesInfo.setCarLicense(carLicense.getText().toString());
				vehiclesInfo.setEngineModel(engineModel.getText().toString());
				List<ResultBean> resultBeans = new QueryVehicleViolation()
						.queryResult(vehiclesInfo);
				for (ResultBean resultBean : resultBeans) {
					resultMessage += resultBean.getTime() + "|"
							+ resultBean.getAddress() + "|"
							+ resultBean.getReason() + "|"
							+ resultBean.getStatus() + "\n\n";
				}
				
				Message message = handler.obtainMessage();
				handler.sendMessage(message);
			}
		}).start();
	}

}